﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Linq.Expressions;

#region Expert
namespace backoffice.Data
{
    partial class Expert
    {
        public void setExpert(Expert e)
        {
            this.ExpertID = e.ExpertID;
            this.UserID = e.UserID;
            this.PathologyID = e.PathologyID;
            this.MethodID = e.MethodID;
            this.PlantID = e.PlantID;
        }
    }
}
#endregion

#region dataExpert
namespace backoffice.Data
{
    public class dataExpert
    {
        #region Create
        public static void createExpert(Expert e)
        {
            try
            {
                db2growDataContext db = new db2growDataContext();

                e.ExpertID = Guid.NewGuid();

                db.Experts.InsertOnSubmit(e);
                db.SubmitChanges();
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        #endregion

        #region Get
        public static Expert getExpert(Guid guid)
        {
            try
            {
                db2growDataContext db = new db2growDataContext();

                Expert e = db.Experts.FirstOrDefault(exp => exp.ExpertID == guid);

                return e;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        public static IQueryable<Expert> getExperts(Expression<Func<Expert, bool>> whereClause)
        {
            try
            {
                db2growDataContext db = new db2growDataContext();

                return db.Experts.Where(whereClause);
            }
            catch (Exception e)
            {
                throw e;
            }
        }
        #endregion

        #region Set
        public static Boolean changeExpert(Expert e)
        {
            try
            {
                if (e == null) return false;
                else
                {
                    db2growDataContext db = new db2growDataContext();

                    Expert res = db.Experts.FirstOrDefault(exp => exp.ExpertID == e.ExpertID);

                    if (res == null) return false;
                    else
                    {
                        res.setExpert(e);

                        db.SubmitChanges();

                        return true;
                    }
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        #endregion

        #region Delete
        public static Boolean deleteExpert(Expert e)
        {
            try
            {
                db2growDataContext db = new db2growDataContext();

                if (db.Experts.Contains(e))
                {
                    Expert res = db.Experts.FirstOrDefault(exp => exp.ExpertID == e.ExpertID);

                    if (res == null)
                    {
                        return false;
                    }
                    else
                    {
                        res.State = false;

                        db.SubmitChanges();

                        return true;
                    }
                }
                else
                {
                    return false;
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        #endregion
    }
}
#endregion